package com.ynr.voucher.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.ynr.voucher.dao.IVoucherDao;
import com.ynr.voucher.model.ExpenseModel;
import com.ynr.voucher.model.FaModel;
import com.ynr.voucher.model.InvoiceItemModel;
import com.ynr.voucher.model.InvoiceModel;
import com.ynr.voucher.model.SubjectModel;
import com.ynr.voucher.model.VoucherModel;
import com.ynr.voucher.service.IFADepreciationService;
import com.ynr.voucher.service.ISubjectService;
import com.ynr.voucher.service.IVoucherService;

@Service
public class VoucherServiceImpl implements IVoucherService {
	
	@Autowired
	private ISubjectService subjectService;
	
	@Autowired
	private IVoucherDao voucherDao;
	
	@Autowired
	private IFADepreciationService faDepreciationService;

	@Override
	public void generateInvoiceVoucher(String billDate, int voucherType, InvoiceModel invoice, Map<String, Object> companyMap) {
		Map<String, Integer> retMap = voucherDao.getLastVoucherNum(billDate, "invoice");
		int lastVoucherNum = 0;
		if(retMap != null && !retMap.isEmpty()) {
			lastVoucherNum = retMap.get("lastVoucherNum");
		}
		int voucherNum = lastVoucherNum+1;
		int companyType = (int)companyMap.get("type");
		int exchangeDetail = (int)companyMap.get("exchange_detail");
		long companyId = (long)companyMap.get("id");
		String supplyName = invoice.getXfMc();
		Double jshj = Double.valueOf(invoice.getSumamount());
		for(InvoiceItemModel item : invoice.getGoodsData()) {
			VoucherModel voucher = new VoucherModel();
			voucher.setCompany_id(companyId);
			voucher.setTable_name("invoice");
			voucher.setTable_id(invoice.getId());
			voucher.setType("invoice");
			voucher.setBill_date(billDate);
			voucher.setVoucher_type(voucherType);   //1-转字凭证
			//自动采番TODO
			voucher.setVoucher_num(voucherNum);
			
			voucher.setAnnex_mount(1); 
			voucher.setAbstract_str(item.getName());
			voucher.setSubjectList(subjectService.generateInvoiceSubjectList(invoice, companyMap, item, item.isFa()));
			voucher.setSupplier(invoice.getXfMc());
			voucher.setOriginator("龚嘉玺");
			voucherDao.addVoucher(voucher);
		}
		if(invoice.getFplx().equals("01") && companyType == 2) {
			String subject = "22210101";
			double tax = 0.0;
			for(InvoiceItemModel item : invoice.getGoodsData()) {
				tax += Double.valueOf(item.getTax_sum());
			}
			SubjectModel taxDebitModel = new SubjectModel(subject, ""+tax, "");
			VoucherModel voucher = new VoucherModel();
			voucher.setCompany_id(companyId);
			voucher.setTable_name("invoice");
			voucher.setTable_id(invoice.getId());
			voucher.setType("invoice");
			voucher.setBill_date(billDate);
			voucher.setVoucher_type(voucherType);   //1-转字凭证
			voucher.setVoucher_num(voucherNum);
			voucher.setAnnex_mount(1); 
			voucher.setAbstract_str("进项税额");
			List<SubjectModel> subList = Lists.newArrayList();
			subList.add(taxDebitModel);
			voucher.setSubjectList(subList);
			voucher.setSupplier(invoice.getXfMc());
			voucher.setOriginator("龚嘉玺");
			voucherDao.addVoucher(voucher);
		}
		List<SubjectModel> creditSubjectList = Lists.newArrayList();
		if(exchangeDetail == 1) {
			creditSubjectList.addAll(subjectService.invoiceCreditSubjectMatch(companyId, supplyName, billDate, jshj));
		} else {
			creditSubjectList.add(new SubjectModel("2202","", ""+jshj));
		}
		VoucherModel voucher = new VoucherModel();
		voucher.setCompany_id(companyId);
		voucher.setTable_name("invoice");
		voucher.setTable_id(invoice.getId());
		voucher.setType("invoice");
		voucher.setBill_date(billDate);
		voucher.setVoucher_type(voucherType);   //1-转字凭证
		voucher.setVoucher_num(voucherNum);
		voucher.setAnnex_mount(1); 
		voucher.setAbstract_str(supplyName);
		voucher.setSubjectList(creditSubjectList);
		voucher.setSupplier(invoice.getXfMc());
		voucher.setOriginator("龚嘉玺");
		voucherDao.addVoucher(voucher);
	}

	@Override
	public void generateSalesVoucher(String billDate, int voucherType, InvoiceModel invoice, 
			Map<String, Object> companyMap, InvoiceItemModel item, int lastVoucherNum) {
		VoucherModel voucher = new VoucherModel();
		voucher.setCompany_id((long)companyMap.get("id"));
		voucher.setTable_name("invoice");
		voucher.setTable_id(invoice.getId());
		voucher.setType("sales");
		voucher.setBill_date(billDate);
		voucher.setVoucher_type(voucherType);   //1-转字凭证
		voucher.setVoucher_num(lastVoucherNum);
		voucher.setAnnex_mount(1); 
		voucher.setAbstract_str("销售" + invoice.getGfMc() + item.getName());
		voucher.setSubjectList(subjectService.generateSalesSubjectList(invoice, companyMap, item));
		voucher.setSupplier(invoice.getXfMc());
		voucher.setOriginator("龚嘉玺");
		voucherDao.addVoucher(voucher);
	}

	@Override
	public void generateExpenseVoucher(Map<String, Object> companyMap, ExpenseModel expense) {
		Map<String, Integer> retMap = voucherDao.getLastVoucherNum(expense.getBill_date(), "invoice");
		int lastVoucherNum = 0;
		if(retMap != null && !retMap.isEmpty()) {
			lastVoucherNum = retMap.get("lastVoucherNum");
		}
		int voucherNum = lastVoucherNum+1;
		VoucherModel voucher = new VoucherModel();
		voucher.setTable_name("expense");
		voucher.setTable_id(expense.getId());
		voucher.setType("expense");
		voucher.setCompany_id((long)companyMap.get("id"));
		voucher.setBill_date(expense.getBill_date());
		voucher.setVoucher_type(1);   //1-转字凭证
		voucher.setVoucher_num(voucherNum);
		voucher.setAnnex_mount(1); 
		voucher.setAbstract_str("报销" + expense.getExpense_type());
		voucher.setSubjectList(subjectService.generateExpenseSubjectList(companyMap, expense));
		voucher.setOriginator("龚嘉玺");
		voucherDao.addVoucher(voucher);
	}

	
	@Override
	public void generateBankstatementVoucher(Map<String, Object> companyMap, int voucherType, String billDate,
			List<Map<String, String>> bankStatementContent) {
		for(Map<String, String> bsItem : bankStatementContent) {
			Map<String, Integer> retMap = voucherDao.getLastVoucherNum(billDate, "bank");
			int lastVoucherNum = 0;
			if(retMap != null && !retMap.isEmpty()) {
				lastVoucherNum = retMap.get("lastVoucherNum");
			} else {
				retMap = voucherDao.getLastVoucherNum(billDate, "salary");
				if(retMap != null && !retMap.isEmpty()) {
					lastVoucherNum = retMap.get("lastVoucherNum");
				} else {
					retMap = voucherDao.getLastVoucherNum(billDate, "sales");
					if(retMap != null && !retMap.isEmpty()) {
						lastVoucherNum = retMap.get("lastVoucherNum");
					} else {
						retMap = voucherDao.getLastVoucherNum(billDate, "invoice");
						if(retMap != null && !retMap.isEmpty()) {
							lastVoucherNum = retMap.get("lastVoucherNum");
						} 
					}
				}
			}
			int voucherNum = lastVoucherNum+1;
			VoucherModel voucher = new VoucherModel();
			voucher.setTable_name("bank_statement_detail");
			voucher.setTable_id(Long.valueOf(bsItem.get("id")));
			voucher.setType("bank");
			voucher.setCompany_id((long)companyMap.get("id"));
			voucher.setBill_date(billDate);
			voucher.setVoucher_type(voucherType);   //2-银字凭证
			voucher.setVoucher_num(voucherNum);
			voucher.setAnnex_mount(1); 
			String abstract_ = bsItem.get("abstract_");
			String purpose = bsItem.get("purpose");
			String remark = bsItem.get("remark");
			String backup = bsItem.get("backup");
			String abstract_str = abstract_.equals("")?(purpose.equals("")?(remark.equals("")?backup:remark):purpose):abstract_;
			String income = bsItem.get("income");
			String cost = bsItem.get("cost");
			abstract_str = (dateFormat(billDate) + (income.equals("")?"支出":"收到") + bsItem.get("pay_account_name") + abstract_str + (income.equals("")?cost:income)); 
			voucher.setAbstract_str(abstract_str);
			voucher.setSubjectList(subjectService.generateBankSubjectList(bsItem, companyMap, billDate));
			voucher.setOriginator("龚嘉玺");
			voucherDao.addVoucher(voucher);
		}
	}

	@Override
	public void generateSalaryVoucher(String companyId, String billDate, 
			List<Map<String, Object>> salaryContent, int lastVoucherNum) {
		for(Map<String, Object> salaryItem : salaryContent) {
			VoucherModel voucher = new VoucherModel();
			voucher.setTable_name("salary_detail");
			voucher.setTable_id(Long.valueOf(salaryItem.get("id")+""));
			voucher.setType("salary");
			voucher.setCompany_id(Long.valueOf(companyId));
			voucher.setBill_date(billDate);
			voucher.setVoucher_type(1);
			voucher.setVoucher_num(lastVoucherNum);
			voucher.setAnnex_mount(1); 
			voucher.setAbstract_str("计提"+salaryItem.get("name")+dateFormat(billDate)+"工资");
			voucher.setSubjectList(subjectService.generateSalarySubjectList(salaryItem));
			voucher.setOriginator("龚嘉玺");
			voucherDao.addVoucher(voucher);
		}
		
	}
	
	private static String dateFormat(String billDate) {
		return billDate.substring(0,4) + "年" + billDate.substring(4) + "月";
	}

	@Override
	public List<Map<String, Object>> getVoucherByCompanyIdAndBillDate(String companyId, String billDate) {
		return voucherDao.getVoucherByCompanyIdAndBillDate(companyId, billDate);
	}

	@Override
	public void updateFaVoucher(String dict, Map<String, Object> dataMap, FaModel fa, Map<String, Object> companyMap, String billDate) {
		List<Map<String, Object>> voucherList = (List)dataMap.get("voucherList");
		List<Map<String, Object>> itemList = (List)dataMap.get("itemList");
		for(Map<String, Object> voucher : voucherList) {
			String abstract_str = (String)voucher.get("abstract_str");
			if(abstract_str.contains(dict)) {
				Map<String, Object> invoiceItem = new HashMap<>();
				for(Map<String, Object> item : itemList) {
					String name = (String)item.get("name");
					if(name.equals(abstract_str)) {
						invoiceItem = item;
					}
				}
				if(!invoiceItem.isEmpty()) {
					InvoiceItemModel item = JSON.parseObject(JSON.toJSONString(invoiceItem), InvoiceItemModel.class);
					faDepreciationService.generateDepreciation(dataMap, item, companyMap, fa, billDate);
					voucherDao.updateVoucherSubject(item.getId(), "1601");
				}
			}
		}
	}

	@Override
	public void updateVoucher(String dict, Map<String, Object> dataMap, String subject) {
		List<Map<String, Object>> voucherList = (List)dataMap.get("voucherList");
		List<Map<String, Object>> itemList = (List)dataMap.get("itemList");
		for(Map<String, Object> voucher : voucherList) {
			String abstract_str = (String)voucher.get("abstract_str");
			if(abstract_str.contains(dict)) {
				Map<String, Object> invoiceItem = new HashMap<>();
				for(Map<String, Object> item : itemList) {
					String name = (String)item.get("name");
					if(name.equals(abstract_str)) {
						invoiceItem = item;
						break;
					}
				}
				if(!invoiceItem.isEmpty()) {
					voucherDao.updateVoucherSubject(Long.valueOf((int)invoiceItem.get("id")), subject);
				}
			}
		}
	}
	
}
